package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.core.mapper.BorrowInfoMapper;
import com.atguigu.srb.core.pojo.entity.BorrowInfo;
import com.atguigu.srb.core.service.BorrowInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@RestController
@RequestMapping("/api/core/borrowInfo")
public class ApiBorrowInfoController {
    @Resource
    private BorrowInfoService borrowInfoService;
    @GetMapping("/getBorrowInfoStatus")
    public R getBorrowInfoStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = null;
        if(!StringUtils.isEmpty(token)) {
            userId = JwtUtils.getUserId(token);

        }
        Integer borrowInfoStatus = borrowInfoService.getBorrowInfoStatus(userId);
        return R.ok().data("borrowInfoStatus",borrowInfoStatus);
    }

    @PostMapping("/auth/save")
    public R saveBorrowInfo(@RequestBody BorrowInfo borrowInfo,HttpServletRequest request){
        //存到BorrowInfo表、拉屎去
        String token = request.getHeader("token");
        String userId = null;
        if(!StringUtils.isEmpty(token)) {
            userId = JwtUtils.getUserId(token);
        }
        borrowInfoService.saveBorrowInfo(borrowInfo,userId);
        return R.ok();
    }
    @GetMapping("/getBorrowAmount")
    public R getBorrowAmount(HttpServletRequest request){
        String token = request.getHeader("token");
        String userId = null;
        if(!StringUtils.isEmpty(token)) {
            userId = JwtUtils.getUserId(token);

        }
        BigDecimal borrowAmount = borrowInfoService.getBorrowAmount(userId);
        return R.ok().data("borrowAmount",borrowAmount);
    }
}

